package buttons.control;

import buttons.PopUpMenu;
import buttons.view.V_PopUpMenu;
import utilities.EventOwner;

/*********************
PopUP CONTROLLER
**********************/
public class C_PopUpMenu extends EventOwner
{
  private V_PopUpMenu vMenu;
  private PopUpMenu parent;
  
  public C_PopUpMenu(V_PopUpMenu view, PopUpMenu p)
  {
    vMenu = view;
    parent = p;
  }
  
  @Override
  public void onInteract(float mouseXLoc, float mouseYLoc, String interID)
  {    
    if(active) {
      vMenu.setVisibility(true);
      if(interID.equals("leftClick"))
      {
        int index = vMenu.getHighlightIndex();
        if(index > -1) {
            triggerEvent(parent.getMenuItems().get(index).getContents()); // triggers to subscribers
        }
        active = false;
      }
      else if(interID.equals("leftDrag") || interID.equals("") )
      {
        vMenu.checkOver(mouseXLoc, mouseYLoc);
      }
    }
    else vMenu.setVisibility(false);
  }
  
  void activateAt(float mouseXLoc, float mouseYLoc)
  {
    active = true;
    vMenu.setXLoc(mouseXLoc);
    vMenu.setYLoc(mouseYLoc); 
  }
  
  String getHighlightedOption(){
    int index = vMenu.getHighlightIndex();
    if(index > -1) {
      return parent.getMenuItems().get(index).getContents();
    }
    else return "";
  }
}
